package com.loooooo.pm2024.modules.pm.controller;


import com.loooooo.pm2024.data.Result;
import com.loooooo.pm2024.modules.pm.model.Task;
import com.loooooo.pm2024.modules.pm.selector.TaskSelector;
import com.loooooo.pm2024.modules.pm.service.TaskService;
import com.loooooo.pm2024.modules.sys.model.DictCatalog;
import com.loooooo.pm2024.modules.sys.selector.DictCatalogSelector;
import com.loooooo.pm2024.modules.sys.service.DictCatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/pm/task")
public class TaskController {
    @Autowired
    TaskService taskService;
    @Autowired
    DictCatalogService dictCatalogService;

    @RequestMapping("/list")
    String listGet() {
        return "pm/pm.task.list";
    }

    @PostMapping(value = "/list", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result listPost(@RequestBody(required = false) TaskSelector selector) {
        if (selector == null) selector = new TaskSelector();
        DictCatalogSelector catalogSelector = new DictCatalogSelector();
        catalogSelector.setCodeList(Arrays.asList("task.status"));//.getTarget().setCode("issue.status");
        catalogSelector.setWithDictList(1);
        List<DictCatalog> catalogList = dictCatalogService.list(catalogSelector);

        if (selector.getPageSize() != null) {
            if (selector.getPageNo() == null || selector.getPageNo() < 1) {
                selector.setPageNo(1);
            }
            Result result = new Result();
            int count = taskService.count(selector);
            result.set("total", count);
            if (count > (selector.getPageSize() * (selector.getPageNo() - 1))) {
                result.setData(taskService.list(selector));
            }
            result.set("dict", catalogList);
            return result.set("now", System.currentTimeMillis());
        }
        return Result.success(taskService.list(selector)).set("dict", catalogList).set("now", System.currentTimeMillis());

    }

    @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result update(@RequestBody Task task) {
        task.setStatus(null);
        int r = taskService.update(task);
        if (Objects.equals(100, task.getProgress())) {
            taskService.commit(task.getId(), 1);
        }
        return Result.success(task).set("affect", r).set("now", System.currentTimeMillis());

    }

    @PostMapping(value = "/log/add")
    @ResponseBody
    Result update(@RequestParam(value = "taskId") Long taskId, @RequestParam(value = "type") String type) {
        int r = taskService.log(taskId, type);
        return Result.success(r).set("affect", r).set("now", System.currentTimeMillis());

    }

    @PostMapping(value = "/commit", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result commit(@RequestParam("id") Long id, @RequestParam(value = "commit", required = false) Integer committed) {
        if (committed == null) committed = 1;
        int r = taskService.commit(id, committed);
        return Result.success(r).set("affect", r).set("now", System.currentTimeMillis());

    }

    @PostMapping(value = "/approve", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result approve(@RequestParam("id") Long id, @RequestParam(value = "approve", required = false) Integer approve) {
        if (approve == null) approve = 1;
        int r = taskService.approve(id, approve);
        return Result.success(r).set("affect", r).set("now", System.currentTimeMillis());

    }

    @PostMapping(value = "/hang", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result hang(@RequestParam("id") Long id, @RequestParam(value = "hang", required = false) Integer hang, @RequestParam(value = "reason", required = false) String reason) {
        if (hang == null) hang = 1;
        int r = taskService.hang(id, hang, reason);
        return Result.success(r).set("affect", r).set("now", System.currentTimeMillis());

    }

    @PostMapping(value = "/giveup", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result giveup(@RequestParam("id") Long id, @RequestParam(value = "giveup", required = false) Integer giveup,
                  @RequestParam(value = "reason", required = false) String reason) {
        if (giveup == null) giveup = 1;
        int r = taskService.giveup(id, giveup, reason);
        return Result.success(r).set("affect", r).set("now", System.currentTimeMillis());

    }

    @PostMapping(value = "/dependent", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    Result dependent(@RequestParam("id") Long id, @RequestParam(value = "dependent", required = false) Integer dependent,
                     @RequestParam(value = "reason", required = false) String reason) {
        if (dependent == null) dependent = 1;
        int r = taskService.dependent(id, dependent, reason);
        return Result.success(r).set("affect", r).set("now", System.currentTimeMillis());

    }

}
